﻿package plugins.mstandio.UniversalMap.cameras {
	 	
	/**
	 * ...
	 * @author mstandio
	 */		
	
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import org.papervision3d.cameras.CameraType;	
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.objects.primitives.Cone;
	import org.papervision3d.render.BasicRenderEngine;	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.scenes.Scene3D;	
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;	
		
	
	public class Cam_orb extends Cam {					
		
		private var bitmapa:BitmapMaterial;		
		private var color:ColorMaterial;				
		
		private var sphere:Sphere;
		private var cone:Cone;				
		private var tripod:DisplayObject3D;
		private var tripodCone:DisplayObject3D;		
		
		private var camera:Camera3D;
		private var viewport:Viewport3D; 
		private var renderer:BasicRenderEngine;
		private var scene:Scene3D;
		
		[Embed(source = '../images/cameras/orb.jpg')]
			private var Bitmap_cameraOrb:Class;
			private var cameraOrb:Bitmap = new Bitmap_cameraOrb();						
		
				
		public function Cam_orb(initPan:Number, initTilt:Number, initZoom:Number, camSize:Number, panShift:Number, camColor:String):void {
			super(initPan, initTilt, initZoom, camSize, panShift, camColor);			
			if (stage) stageReady();
			else addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);      
		}
		
		private function stageReady(e:Event = null):void {            
			removeEventListener(Event.ADDED_TO_STAGE, stageReady);		
			
			this.viewport = new Viewport3D(350, 350, false, false, false, true); 
						
			this.viewport.x = this.viewport.y = -175;
			this.addChild(viewport);
			
			this.renderer = new BasicRenderEngine();									
			this.scene = new Scene3D();
			
			this.camera = new Camera3D();
			this.camera.zoom = 13*this.camSize; 
			this.camera.focus = 100; 						             
            			
			this.bitmapa = new BitmapMaterial(this.cameraOrb.bitmapData); 			 			 			 
			
			this.color = new ColorMaterial(Number(this.camColor), 0.7);						
						
			this.sphere = new Sphere(bitmapa, 12, 8, 6);					
			this.sphere.rotationZ = -this.initPan - this.panShift;
			this.sphere.rotationY = -this.initTilt+180;
			
			tripod = new DisplayObject3D();
			tripod.addChild(sphere);
			scene.addChild(tripod);			
			tripod.rotationX += 20;			
			
			tripodCone = new DisplayObject3D();
			sphere.addChild(tripodCone);				
			tripodCone.scaleZ = stage.stageHeight / stage.stageWidth +0.2;						
			
			this.rotate(this.initPan, this.initTilt);
			this.drawCone(this.initZoom);							
		}		
		
		
		
		private function drawCone(zoom:Number):void {
			if(this.cone!=null){
				this.tripodCone.removeChild(cone);									
				this.cone = null;
			}			
			
			this.cone = new Cone(color, 3.5 * (20-zoom), 45, 4, 1);						
			this.cone.x -= 33;						
			this.cone.rotationZ -= 90;
			this.cone.rotationY -= 45;								
			this.tripodCone.addChild(cone);									
			
			this.renderer.renderScene(scene, camera, viewport);				
		}		
		
		
		public override function rotate(pan:Number, tilt:Number):void {									
			this.sphere.rotationZ = -pan - this.panShift;
			this.sphere.rotationY = -tilt+180;
			this.renderer.renderScene(scene, camera, viewport);				
			this.initPan = pan;
			this.initTilt = tilt;
		}
		
		public override function zoom(zoom:Number):void {						
			this.initZoom = this.validateZoom(zoom);
			this.drawCone(this.initZoom);			
		}				
	}	
}